/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import pojo.DonDatHang_pojo;
import util.MySQLDataAccessHelper;

/**
 *
 * @author Rocker000
 */
public class DonDatHang_dao 
{
    
    public static void XacNhanDaThanhToan(int madondathang)
    {
        String sql="update dondathang set dathanhtoan = 1 where madondathang="+madondathang+" ";
        try
        {
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.Open();
            helper.ExeCuteUpdate(sql);
            helper.Close();
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
    }
    
    
    public static ArrayList<DonDatHang_pojo> LayDanhSach(int dathanhtoan)
    {               
        ArrayList<DonDatHang_pojo> ds = new ArrayList<DonDatHang_pojo>();
        try
        {            
            String sql ="select*from dondathang where dathanhtoan ="+dathanhtoan+"";
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.Open();
            ResultSet rs = helper.ExeCuteQuery(sql);
            while(rs.next())
            {
                DonDatHang_pojo temp = new DonDatHang_pojo();
                temp.setMadondathang(rs.getInt("madondathang"));
                temp.setDatthanhtoan(rs.getInt("dathanhtoan"));
                temp.setIsdeleted(rs.getInt("isdeleted"));
                temp.setNgaydathang(rs.getString("ngaydathang"));
                temp.setTendangnhap(rs.getString("tendangnhap"));
                temp.setTenkhachhang(rs.getString("tenkhachhang"));
                temp.setSoluong(rs.getInt("soluong"));
                temp.setMasanpham(rs.getString("masanpham"));
                temp.setSodienthoai(rs.getString("sodienthoai"));
                temp.setDiachi(rs.getString("diachi"));
                temp.setIsdeleted(rs.getInt("isdeleted"));
                
                
                
                ds.add(temp);
            }
            helper.Close();
                
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        return ds;        
    }
    public static boolean ThemDonDatHang(DonDatHang_pojo x)
    {
        boolean kq= false;                
        try
        {
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.Open();
            String sql = String.format("INSERT INTO `sieuthidienmay`.`dondathang` (`tenkhachhang`, `tendangnhap`, `ngaydathang`, `email`,`diachi`,`sodienthoai`,masanpham,soluong,dathanhtoan) VALUES ('%s', '%s', '%s', '%s', '%s', '%s','%s',%d,%d)",x.getTenkhachhang(),x.getTendangnhap(),x.getNgaydathang(),x.getEmail(),x.getDiachi(),x.getSodienthoai(),x.getMasanpham(),x.getSoluong(),0);
            int n = helper.ExeCuteUpdate(sql);
            if(n==1)
                kq= true;            
            helper.Close();
        }
        catch(Exception ex)
        {
            System.err.println(ex.getMessage());
        }       
        return kq;
    }
    public static int LayMa(String email,String sodienthoai,String ngaydathang)
    {
        int ma=-1;
        String sql = "select*from dondathang where email="+email+" and sodienthoai="+sodienthoai+"and ngaydathang='"+ngaydathang+"'";
        try
        {
            DonDatHang_pojo d = new DonDatHang_pojo();
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.Open();
            ResultSet rs = helper.ExeCuteQuery(sql);
            if(rs.next())
                ma = rs.getInt("madondathang");                           
            helper.Close();
        }
        catch(Exception ex)
        {
            System.err.println(ex.getMessage());
        } 
        return ma;
    }
    
}
